{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [NumPy-菜鸟教程](https://www.runoob.com/numpy/numpy-ndarray-object.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [NumPy Ndarray 对象](https://www.runoob.com/numpy/numpy-ndarray-object.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "创建一个 ndarray 只需调用 NumPy 的 array 函数即可：\n",
    "`numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)\n",
    "`\n",
    "\n",
    "|名称|\t描述|\n",
    "|----:|--------:|\n",
    "|object|\t数组或嵌套的数列|\n",
    "|dtype\t|数组元素的数据类型，可选|\n",
    "|copy\t|对象是否需要复制，可选|\n",
    "|order\t|创建数组的样式，C为行方向，F为列方向，A为任意方向（默认）|\n",
    "|subok\t|默认返回一个与基类类型一致的数组|\n",
    "|ndmin\t|指定生成数组的最小维度|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,6]])\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2 3]\n",
      "  [4 5 6]]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,6]], ndmin = 3)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.+0.j 2.+0.j 3.+0.j]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3], dtype = complex)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [NumPy 数据类型](https://www.runoob.com/numpy/numpy-dtype.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下表列举了常用 NumPy 基本类型。\n",
    "\n",
    "|名称|\t描述|\n",
    "|----:|-------:|\n",
    "|bool_|\t布尔型数据类型（True 或者 False）|\n",
    "|int_|\t默认的整数类型（类似于 C 语言中的 long，int32 或 int64）|\n",
    "|intc|\t与 C 的 int 类型一样，一般是 int32 或 int 64|\n",
    "|intp|\t用于索引的整数类型（类似于 C 的 ssize_t，一般情况下仍然是 |int32 或 int64）|\n",
    "|int8|\t字节（-128 to 127）|\n",
    "|int16|\t整数（-32768 to 32767）|\n",
    "|int32|\t整数（-2147483648 to 2147483647）|\n",
    "|int64|\t整数（-9223372036854775808 to 9223372036854775807）|\n",
    "|uint8|\t无符号整数（0 to 255）|\n",
    "|uint16|无符号整数（0 to 65535）|\n",
    "|uint32|无符号整数（0 to 4294967295）|\n",
    "|uint64|无符号整数（0 to 18446744073709551615）|\n",
    "|float_|float64 类型的简写|\n",
    "|float16|半精度浮点数，包括：1 个符号位，5 个指数位，10 个尾数位|\n",
    "|float32|单精度浮点数，包括：1 个符号位，8 个指数位，23 个尾数位|\n",
    "|float64|双精度浮点数，包括：1 个符号位，11 个指数位，52 个尾数位|\n",
    "|complex_|complex128 类型的简写，即 128 位复数|\n",
    "|complex64|复数，表示双 32 位浮点数（实数部分和虚数部分）|\n",
    "|complex128|复数，表示双 64 位浮点数（实数部分和虚数部分）|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数据类型对象 (dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dtype 对象是使用以下语法构造的：\n",
    "\n",
    "`numpy.dtype(object, align, copy)`\n",
    "- object - 要转换为的数据类型对象\n",
    "- align - 如果为 true，填充字段使其类似 C 的结构体。\n",
    "- copy - 复制 dtype 对象 ，如果为 false，则是对内置数据类型对象的引用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n"
     ]
    }
   ],
   "source": [
    "dt = np.dtype(np.int32)\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "# int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替\n",
    "dt = np.dtype('i8')\n",
    "print(dt)\n",
    "\n",
    "# 字节顺序标注\n",
    "dt_ = np.dtype('<i4')\n",
    "print(dt_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面实例展示结构化数据类型的使用，类型字段和对应的实际类型将被创建。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('age', 'i1')]\n",
      "[(10,) (20,) (30,)]\n",
      "[10 20 30]\n"
     ]
    }
   ],
   "source": [
    "# 首先创建结构化数据类型\n",
    "dt = np.dtype([('age', np.int8)])\n",
    "print(dt)\n",
    "\n",
    "# 将数据类型应用于 ndarray 对象\n",
    "a = np.array([(10,), (20,), (30,)], dtype = dt)\n",
    "print(a)\n",
    "\n",
    "# 类型字段名可以用于存取实际的 age 列\n",
    "print(a['age'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]\n"
     ]
    }
   ],
   "source": [
    "student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])\n",
    "print(student)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
